package net.dreamlu.common.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

public class FreemarkerUtil {
	private static FreemarkerUtil instance;;
	private static Configuration cfg;

	private FreemarkerUtil() {
	}

	public static FreemarkerUtil getInstance() {
		if (instance == null) {
			synchronized (FreemarkerUtil.class) {
				if (instance == null) {
					cfg = new Configuration(Configuration.VERSION_2_3_23);
					cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(100, 2500));
					instance = new FreemarkerUtil();
				}
			}
		}
		cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/sqls/");
		return instance;
	}

	/**
	 * @param freemarkerVersionNo freemarker的版本号
	 * @param templatePath        模版加载路径
	 * @return
	 */
	public static FreemarkerUtil getInstance(String templatePath) {
		if (instance == null) {
			synchronized (FreemarkerUtil.class) {
				if (instance == null) {
					cfg = new Configuration(Configuration.VERSION_2_3_23);
					cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(100, 2500));
					instance = new FreemarkerUtil();
				}
			}
		}
		cfg.setClassForTemplateLoading(FreemarkerUtil.class, templatePath);
		return instance;
	}

	/**
	 *
	 * @param templateName 根据模版名称加载对应模版
	 * @return Template
	 */
	private Template getTemplate(String templateName) {
		try {
			return cfg.getTemplate(templateName);
		} catch (TemplateNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedTemplateNameException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * @param dataModel    数据模型
	 * @param templateName 模拟名字
	 */
	public static String sql2String(Map<String, Object> dataModel, String templateName) {
		try {
			StringWriter sw = new StringWriter();
			getInstance().getTemplate(templateName).process(dataModel, sw);
			return sw.toString();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param dataModel    数据模型
	 * @param templateName 模拟名字
	 */
	public String sprint(Map<String, Object> dataModel, String templateName) {
		try {
			StringWriter sw = new StringWriter();
			this.getTemplate(templateName).process(dataModel, sw);
			return sw.toString();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 *
	 * @param dataModel    数据模型
	 * @param templateName 输出模版
	 * @param filePath     输出文件路径
	 */
	public void fprint(Map<String, Object> dataModel, String templateName, String filePath) {
		try {
			this.getTemplate(templateName).process(dataModel, new FileWriter(new File(filePath)));
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
